package ru.myrtle.umf.tools.ugen.templates;

import java.io.*;
import java.text.*;
import java.util.*;
import ru.myrtle.umf.*;
import ru.myrtle.umf.codegen.*;
import ru.myrtle.umf.codegen.java.*;
import ru.myrtle.umf.tools.ugen.*;

public class UEnumerationDelegateImplTemplate extends UTemplate
{
    protected final String NL = System.getProperties().getProperty("line.separator");
    protected final String TEXT_1 = "package ";
    protected final String TEXT_2 = ";" + NL + NL + "public class ";
    protected final String TEXT_3 = " implements ";
    protected final String TEXT_4 = " {" + NL;
    protected final String TEXT_5 = NL + "}" + NL;
    protected final String TEXT_6 = NL;

    /*
     *
     */


    public UEnumerationDelegateImplTemplate() {

    }

    /*
     *
     */

    public String generate(UTemplateContext context) throws UTemplateRejectException
    {
        final StringBuffer stringBuffer = new StringBuffer();

    checkTemplateObjectType(context, UEnumerationLiteral.class);

    UEnumerationLiteral object = (UEnumerationLiteral) context.getObject();
    UEnumeration enumeration = object.getEnumeration();

    if (enumeration.getOperations().isEmpty()) {
        reject();
    }

    String enumDelegateClassName     = UGenUtil.getEnumerationDelegateClassName(enumeration);
    String enumDelegateClassPackage  = UGenUtil.getEnumerationDelegateClassPackage(enumeration);

    String enumDelegateImplClassName     = UGenUtil.getEnumerationDelegateImplClassName(object);
    String enumDelegateImplClassPackage  = UGenUtil.getEnumerationDelegateImplClassPackage(object);

    context.getResource().setObjectFileName(enumDelegateImplClassName);
    context.getResource().setObjectFileExtension("java");
    context.getResource().setObjectPackage(enumDelegateImplClassPackage);

    JavaImportBuilder importBuilder = new JavaImportBuilder();

    importBuilder.addPackage(enumDelegateClassPackage);

    String classBody = NL;

    JavaFile javaFile = new JavaFile();
    javaFile.decompose(context.getResource());

    classBody = javaFile.getCode();

    for (String importLine : javaFile.getImports()) {
        importBuilder.addObjectPackage(importLine);
    }

    classBody += NL;

        stringBuffer.append(TEXT_1);
        stringBuffer.append(enumDelegateImplClassPackage);
        stringBuffer.append(TEXT_2);
        stringBuffer.append(enumDelegateImplClassName);
        stringBuffer.append(TEXT_3);
        stringBuffer.append(enumDelegateClassName);
        stringBuffer.append(TEXT_4);
        stringBuffer.append(classBody);
        stringBuffer.append(TEXT_5);

    insertImport(stringBuffer, importBuilder, enumDelegateImplClassPackage);

        stringBuffer.append(TEXT_6);

        return stringBuffer.toString();
    }

    // ---------------------------------------------------------------------------------------
    // ---------------------------------------------------------------------------------------

}
